package com.lottery.im.module.setting.ui;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.lottery.im.MyApplication;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.UserInfo;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.setting.presenter.AddPwdPresenter;
import com.lottery.im.module.setting.view.AddPwdView;

/*
 *  首次设置 资金密码
 * */
public class AddMoneyPwdFragment extends BaseSwipeFragment<AddPwdPresenter> implements
    AddPwdView {

  @BindView(R.id.tv_title) TextView mTvTitle;
  @BindView(R.id.et_pwd1) EditText mEtPwd1;
  @BindView(R.id.et_pwd2) EditText mEtPwd2;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_add_money_pwd;
  }

  @Override protected void initPresenter() {
    presenter = new AddPwdPresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    mTvTitle.setText("设置密码");
  }

  @OnClick({ R.id.iv_back, R.id.btn_save }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.btn_save:
        submit();
        break;
    }
  }

  private void submit() {

    String pwd = mEtPwd1.getText().toString();
    if (TextUtils.isEmpty(pwd)) {
      showToast("请输入新密码");
      return;
    }
    if (pwd.length() != 6) {
      showToast("密码由6位数字组成");
      return;
    }
    String pwd2 = mEtPwd2.getText().toString();
    if (TextUtils.isEmpty(pwd2)) {
      showToast("请再次确认密码");
      return;
    }
    if (pwd2.length() != 6) {
      showToast("密码由6位数字组成");
      return;
    }
    if (!pwd.equals(pwd2)) {
      showToast("两次密码输入不一致");
      return;
    }
    presenter.updateWithdrawPassword(pwd);
  }

  @Override public void updatePasswordSuc() {
    showToast("设置成功");
    UserInfo userInfo = MyApplication.getInstance().getUserInfo();
    userInfo.setIsSetPayPwd("Y");
    UserInfo.refreshInfo(mActivity, userInfo);
    popBackStack();
  }
}
